<?php
/**
 * crm帐号生成并导出
 */
apf_require_controller("Abstract");
apf_require_class('Util_Chinese2Pinyin');
apf_require_class('Const_FirstName');
apf_require_class('Bll_Flow_UserSame');
class Flow_OutputController extends AbstractController {

    public function handle_request() {
        $param = $this->request->get_parameters();
        $role_list = $this->user_info['role_list'];
        $city_ids = $role_list[Const_Flow::ROLE_IT];

        $param_check = false;
        if(isset($param['start']) && isset($param['end'])){
            if(trim($param['start']) && trim($param['end'])){
                $param_check = true;
            }
        }elseif(!isset($param['start']) && !isset($param['end'])){
            $param_check = true;
        }
        if(!$param_check){
               $this->show_tips('ERROR_PARAM',"/flow/",Const_Tip::ERROR);
        }
        if($city_ids){
            $pinyin = new Util_Chinese2Pinyin();
            //新入职用户
            $users = $this->_get_bll_flow_user()->_get_entrys_now($city_ids,$param);
            //已存在帐号和邮箱
            $accounts = $this->_get_bll_hroa()->get_all_mail_account();
            //未确认的刚导入的帐号和邮箱
            $accounts_new = $this->_get_bll_flow_user()->get_all_mail_account();
            $accounts = $this->_build_account($accounts,$accounts_new);
            //所有姓氏
            $first_names = Const_FirstName::get_all_firstname();
            foreach ($users as $key=>$user){
                //拆分姓名
                $first_last = $this->_get_name($user['real_name'], $first_names);
                $users[$key]['xing'] = $first_last['first'];
                $users[$key]['ming'] = $first_last['last'];
                if($user['domain_account'] || $user['office_mail']){
                    continue;
                }
                //转换为拼音
                $temp = $pinyin->get_pinyin_all($first_last['first']);
                $xing = strtolower($temp['str_all']);
                $temp = $pinyin->get_pinyin_all($first_last['last']);
                $ming = strtolower($temp['str_all']);
                //可能邮箱
                $mail = $user['english_name'].$xing."@anjuke.com";
                $mail2= $ming.$xing."@anjuke.com";
                $mail = strtolower($mail);
                $mail2 = strtolower($mail2);
                //生成邮箱和域帐号
                if($user['english_name'] && !in_array($user['english_name'].$xing, $accounts['domain_account']) && !in_array($mail, $accounts['office_mail'])){
                    $users[$key]['domain_account'] = $user['english_name'].$xing;
                    $users[$key]['office_mail'] = $mail;

                    $accounts['domain_account'][] =  $user['english_name'].$xing;
                    $accounts['office_mail'][] = $mail;
                }elseif($ming && $xing && !in_array($ming.$xing, $accounts['domain_account']) && !in_array($mail2, $accounts['office_mail'])){
                    $users[$key]['domain_account'] = $ming.$xing;
                    $users[$key]['office_mail'] = $mail2;

                    $accounts['domain_account'][] =  $ming.$xing;
                    $accounts['office_mail'][] = $mail2;
                }else{
                    $samenum = Bll_Flow_UserSame::get_instance()->get_user_same_num();
                    $users[$key]['domain_account'] = $ming.$xing.'_'.$samenum;
                    $users[$key]['office_mail'] = $ming.$xing.'_'.$samenum."@anjuke.com";

                    $accounts['domain_account'][] =  $ming.$xing.'_'.$samenum;
                    $accounts['office_mail'][] = $ming.$xing.'_'.$samenum."@anjuke.com";
                }
                //CRM
                /*if(!in_array($user['real_name'], $accounts['crm_account'])){
                    $users[$key]['crm_account'] = $user['real_name'];

                    $accounts['crm_account'][] = $user['real_name'];
                }else{
                    $users[$key]['crm_account'] = '&nbsp;';
                }*/
            }
            $this->_destroy($accounts);
            $this->_destroy($first_names);
            //获取城市部门信息
            $users = $this->_get_user_info($users);
            //更新到数据库
            $this->_update_to_entry($users);
            //输出到页面
            $this->_output_data($users);
        }else{
            $this->show_tips('ERROR_AUTH',"/flow/",Const_Tip::ERROR);
        }
    }
    /**
     * 合并数据
     */
    private function _build_account($accounts,$accounts_new){
        $result = array();
        foreach($accounts as $val){
            /*if($val['crm_account']){
                $result['crm_account'][]=$val['crm_account'];
            }*/
            if($val['domain_account']){
                $result['domain_account'][]=$val['domain_account'];
            }
            if($val['office_mail']){
                $result['office_mail'][]=$val['office_mail'];
            }
        }
        foreach($accounts_new as $val){
            /*if($val['crm_account']){
                $result['crm_account'][]=$val['crm_account'];
            }*/
            if($val['domain_account']){
                $result['domain_account'][]=$val['domain_account'];
            }
            if($val['office_mail']){
                $result['office_mail'][]=$val['office_mail'];
            }
        }
        $this->_destroy($accounts);
        $this->_destroy($accounts_new);
        return $result;
    }
    /**
     * 同步到entry
     */
    private function _update_to_entry($users){
        foreach($users as $user){
            $data = array(
                    //'crm_account'=>$user['crm_account']=='&nbsp;'?"":$user['crm_account'],
                    'domain_account'=>$user['domain_account']=='&nbsp;'?"":$user['domain_account'],
                    'office_mail'=>$user['office_mail']=='&nbsp;'?"":$user['office_mail']
            );
            $this->_get_bll_flow_user()->update_flow_user($user['entry_id'], $data);
        }
    }
    /**
     * 获取城市，部门信息
     */
    private function _get_user_info($users){
        $job_ids = array();
        foreach($users as $user){
            $job_ids[] = $user['job_id'];
        }
        $job_dep = $this->_get_bll_hroa()->get_job_dep_infos($job_ids);
        foreach ($users as $key => $user){
            $users[$key]['department_name'] = $job_dep[$user['job_id']]['department_name'];
            $users[$key]['bu_name'] = $job_dep[$user['job_id']]['bu_name'];
            $users[$key]['city_name'] = $job_dep[$user['job_id']]['city_name'];
        }
        $this->_destroy($job_dep);
        return $users;
    }
    private function _destroy(&$data){
        $data = null;
        unset($data);
    }
    /**
     * 获取所有已经存在的帐号和邮箱
     */
    private function _get_all_mail_acount(){
        $exit_data = $this->_get_bll_hroa()->get_all_mail_account();
        $mail = array();
        $crm = array();
        $domain = array();
        foreach($exit_data as $user){
            if($user['domain_account']){
                $domain[] = $user['domain_account'];
            }
            /*if($user['crm_account']){
                $crm[] = $user['crm_account'];
            }*/
            if($user['office_mail']){
                $mail[] = $user['office_mail'];
            }
        }
        return array('domain_account'=>$domain,'office_mail'=>$mail);
    }

    /**
     * 拆分姓与名
     */
    private function _get_name($name,&$names){
        $one = substr($name, 0,3);
        $two = substr($name, 0,6);
        if(in_array($one, $names)){
            $first_name = $one;
            $last_name = substr($name, 3);
        }
        if(in_array($two, $names)){
            if(substr($name, 6)){
                $first_name = $two;
                $last_name = substr($name, 6);
            }
        }
        return array('first'=>$first_name,'last'=>$last_name);
    }
    private function _output_data($users){
        //生成帐号
        //导出到excel
        $filename = 'user_account_'.date('Y-m-d').'.xls';
        $dl_index = '<tr align="center">';
        $dl_index .= '<th bgColor="#CCCCCC">姓</th>';
        $dl_index .= '<th bgColor="#CCCCCC">名</th>';
        $dl_index .= '<th bgColor="#CCCCCC">姓名</th>';
        $dl_index .= '<th bgColor="#CCCCCC">系统帐号</th>';
        $dl_index .= '<th bgColor="#CCCCCC">系统密码</th>';
        $dl_index .= '<th bgColor="#CCCCCC">邮箱帐号</th>';
        $dl_index .= '<th bgColor="#CCCCCC">工号</th>';
        $dl_index .= '<th bgColor="#CCCCCC">城市</th>';
        $dl_index .= '<th bgColor="#CCCCCC">部门</th>';
        //$dl_index .= '<th bgColor="#CCCCCC">CRM帐号</th>';
        $dl_index .= '</tr>';
        foreach($users as $user){
            $dl_index .= '<tr align="center">';
            $dl_index .= '<td >'.$user['xing'].'</td>';
            $dl_index .= '<td >'.$user['ming'].'</td>';
            $dl_index .= '<td >'.$user['real_name'].'</td>';
            $dl_index .= '<td >'.$user['domain_account'].'</td>';
            $dl_index .= '<td >3edc1234</td>';
            $dl_index .= '<td >'.$user['office_mail'].'</td>';
            $dl_index .= '<td >'.$user['user_code'].'</td>';
            $dl_index .= '<td >'.$user['city_name'].'</td>';
            $dl_index .= '<td >'.$user['bu_name'].$user['department_name'].'</td>';
            //$dl_index .= '<td >'.$user['crm_account'].'</td>';
            $dl_index .= '</tr>';
        }
        $this->_build_xls($filename,$dl_index);
    }
    /**
     * 输出到页面
     */
    private function _build_xls($filename,$content){
        $xls = <<<EOT
        <html>
        <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
        <title></title>
        </head>
        <body>
        <table border="1" width="1000">
        $content
        </table>
        </body>
        </html>
EOT;
        ob_clean();
        $filename = strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE') !==false ? urlencode($filename) : $filename;
        header("Content-type:   application/vnd.ms-excel");
        header("Content-Transfer-Encoding: binary");
        header("Accept-Ranges:   bytes");
        header("Accept-Length:   ".strlen($xls));
        header("Content-Disposition:   attachment;   filename=" . $filename);
        echo $xls;
        exit;
    }
    /**
     * 入职bll
     * @return Bll_Flow_User
     */
    private function _get_bll_flow_user(){
        if(!$this->bll_flow_user ){
            apf_require_class('Bll_Flow_User');
            $this->bll_flow_user = new Bll_Flow_User();
        }
        return $this->bll_flow_user;
    }
    /**
     * hroa interface bll
     * @return Bll_Hr_Property
     */
    private function _get_bll_hroa(){
        if(!$this->bll_hroa ){
            apf_require_class('Bll_Hr_OutInterface');
            $this->bll_hroa = new Bll_Hr_OutInterface();
        }
        return $this->bll_hroa;
    }
}